package com.quyou.funtour.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quyou.funtour.dao.RegionDao;
import com.quyou.funtour.po.ActivityPo;
import com.quyou.funtour.po.RegionPo;
import com.quyou.funtour.po.UserParticipateActivityPo;
import com.quyou.funtour.service.ActivityService;
import com.quyou.funtour.service.RegionService;
import com.quyou.funtour.service.UserParticipateActivityService;
import com.quyou.funtour.utils.CopyUtils;
import com.quyou.funtour.vo.ActivityVo;
import com.quyou.funtour.vo.RegionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RegionServiceImpl extends ServiceImpl<RegionDao, RegionPo> implements RegionService {

    @Autowired
    private ActivityService activityService;
    @Autowired
    private UserParticipateActivityService userParticipateActivityService;

    /**
     * 获取所有的专区数据
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Override
    public List<RegionVo> getAllRegion()  {
        List<RegionVo> objects = new ArrayList<>();
        List<RegionPo> list = list();
        for (RegionPo regionPo : list) {
            RegionVo regionVo = new RegionVo();
            try {
                BeanUtils.copyProperties(regionPo,regionVo);
                objects.add(regionVo);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return objects;
    }

    /**
     * 根据id获取专区的详细信息
     * @param id
     * @return
     */
    @Override
    public RegionPo getRegionById(String id) {
        //获取专区
        RegionPo regionPo = getById(id);
        if(regionPo!=null){
            CopyUtils<ActivityPo, ActivityVo> activityPoActivityVoCopyUtils = new CopyUtils<>();
            //获取该专区的正在组队的活动
            List<ActivityPo> activityPos = activityService
                    .list(new QueryWrapper<ActivityPo>()
                            .eq("r_id", regionPo.getId())//专区id等于当前专区
                            .ge("end_time",new Date()).orderByDesc("start_time"));//开始时间大于等于当前时间
            //获取vo对象
            List<ActivityVo> activityVoList = activityPoActivityVoCopyUtils.copy(activityPos, ActivityVo.class);

            for (ActivityVo activityVo : activityVoList) {
                List<UserParticipateActivityPo> a_id = userParticipateActivityService.list(new QueryWrapper<UserParticipateActivityPo>().eq("a_id", activityVo.getId()));
                activityVo.setAlreadyPeopleNum(a_id.size());
            }
            regionPo.setHandActivity(activityVoList);

            //获取该专区往期举办的活动
            List<ActivityPo> list = activityService
                    .list(new QueryWrapper<ActivityPo>()
                            .eq("r_id", regionPo.getId())//专区id等于当前专区
                            .le("end_time", new Date()).orderByDesc("start_time"));//结束时间小于等于当前时间
            //获取vo对象
            List<ActivityVo> activityVoList1 = activityPoActivityVoCopyUtils.copy(list, ActivityVo.class);
            for (ActivityVo activityVo : activityVoList1) {
                List<UserParticipateActivityPo> a_id = userParticipateActivityService.list(new QueryWrapper<UserParticipateActivityPo>().eq("a_id", activityVo.getId()));
                activityVo.setAlreadyPeopleNum(a_id.size());
            }
            regionPo.setBeforeActivity(activityVoList1);
        }

        return regionPo;
    }
}
